package top.deepdesigner.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import top.deepdesigner.dto.OrderShipDTO;
import top.deepdesigner.mapper.DtsOrderMapper;
import top.deepdesigner.pojo.DtsOrder;
import top.deepdesigner.service.DtsOrderService;
import top.deepdesigner.vo.CategorySellAmts;
import top.deepdesigner.vo.CommonCatVO;
import top.deepdesigner.vo.DayStatis;
import top.deepdesigner.vo.StatOrderVO;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/20 星期二 19:50:21 <br/>
 */
@Service
public class DtsOrderServiceImpl extends ServiceImpl<BaseMapper<DtsOrder>, DtsOrder> implements DtsOrderService {
  @Resource
  private DtsOrderMapper dtsOrderMapper;

  @Override
  public Long getOrderCount() {
    return this.count(null);
  }

  @Override
  public List<CategorySellAmts> getCategorySell() {
    return dtsOrderMapper.selectCategorySell();
  }

  @Override
  public List<DayStatis> getOrderTotalAndCountByDate(Integer days) {
    return dtsOrderMapper.selectOrderTotalAndCountByDate(days);
  }

  @Override
  public List<DtsOrder> getOrderList(Integer page, Integer limit, String sort, String order, Integer userId, String orderSn, Integer[] orderStatusArray) {
    QueryWrapper<DtsOrder> wrapper = new QueryWrapper<>();
    wrapper
        .eq("deleted", 0)
        .eq(userId != null, "user_id", userId)
        .eq(!StringUtils.isEmpty(orderSn), "order_sn", orderSn)
        .in(orderStatusArray.length > 0, "order_status", Arrays.stream(orderStatusArray).collect(Collectors.toList()))
        .orderBy(true, "asc".equalsIgnoreCase(order), sort);
    Page<DtsOrder> userPage = new Page<>(page, limit);
    return this.page(userPage, wrapper).getRecords();
  }

  @Override
  public List<CommonCatVO<String, String>> getShipChannelList() {
    List<DtsOrder> list = this.list(new LambdaQueryWrapper<DtsOrder>()
        .select(DtsOrder::getShipChannel)
        .isNotNull(DtsOrder::getShipChannel)
        .groupBy(DtsOrder::getShipChannel));
    return list.stream().map(item -> {
      CommonCatVO<String, String> catVo = new CommonCatVO<>();
      catVo.setValue(item.getShipChannel());
      catVo.setLabel(item.getShipChannel());
      return catVo;
    }).collect(Collectors.toList());
  }

  @Override
  public DtsOrder getOrderById(Integer id) {
    return this.getOne(new LambdaQueryWrapper<DtsOrder>()
        .eq(id != null, DtsOrder::getId, id)
        .eq(true, DtsOrder::getDeleted, 0));
  }

  @Override
  public List<StatOrderVO> getOrderByDate(int days) {
    return dtsOrderMapper.selectOrderStat(days);
  }

  @Override
  @Transactional
  public Boolean shipGoods(OrderShipDTO orderShipDTO) {
    DtsOrder order = new DtsOrder();
    order.setUpdateTime(new Date());
    order.setShipSn(orderShipDTO.getShipSn());
    order.setShipTime(new Date());
    order.setShipChannel(orderShipDTO.getShipChannel());
    order.setOrderStatus(orderShipDTO.getOrderStatus());
    return this.update(order, new LambdaUpdateWrapper<DtsOrder>().eq(DtsOrder::getId, orderShipDTO.getOrderId()));
  }

}
